package org.lyl.taobao.controller;

import org.lyl.taobao.api.IUserService;
import org.lyl.taobao.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @Author: Re
 * @Date: 2021/3/30 17:40
 */
@WebServlet("/checkUser")
public class CheckUserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List users = null;
        String checkUser = req.getParameter("check");
        IUserService iUserService = new UserServiceImpl();
        if(checkUser.equals("allUsers")) {
            users = iUserService.checkAllUser();
            req.setAttribute("msg",users);
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        } else if (checkUser.equals("user")) {
            String userName = req.getParameter("checkUserName");
            users =  iUserService.checkUser(userName);
            if (users == null) {
                req.setAttribute("warning","用户名错误");
                req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
                return;
            }
            req.setAttribute("msg",users);
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        }else if (checkUser.equals("deleteUser")){
            String[] strings = req.getParameterValues("deleteUserName");
            if(strings != null){
                for (int i = 0; i < strings.length; i++) {
                    iUserService.deleteUser(strings[i]);
                }
            }
            req.getRequestDispatcher("checkUser.jsp").forward(req,resp);
        }
    }
}
